import React, { Component } from "react";
import {
  Link,
  Route,
  Switch,
  useHistory,
  useLocation,
  useParams,
  useRouteMatch,
} from "react-router-dom";

// hooks是16.8版本新增的，它只能给函数组件用
// 它是让函数组件能够拥有类组件的功能
// withRouter高阶组件可以让组件拥有路由信息,但是它会让组件层级变深
// hooks不会让层级变深
// hooks都是use开头的

// hooks的调用只能写在函数组件的顶层(第一层)
const Home = () => {
  const history = useHistory();
  // console.log(history);

  const location = useLocation();
  console.log(location);
  const params = useParams();
  console.log(params);

  const match = useRouteMatch();
  console.log(match);

  const fn = () => {
    history.push("/about");
  };

  return (
    <div>
      home页面 - <button onClick={fn}>btn</button>
    </div>
  );
};

class About extends Component {
  render() {
    return <div>about页面</div>;
  }
}

class Mine extends Component {
  render() {
    return <div>mine页面</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h3>react-router-hooks</h3>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home">
            <Home />
          </Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
